/**
 * Created : July 4, 2012
 *
 * @author pquiring
 */

import java.io.*;
import javax.swing.*;

import javaforce.*;
import javaforce.gl.*;

public class MainPanel extends javax.swing.JPanel {

  /**
   * Creates new form MainPanel
   */
  public MainPanel() {
    initComponents();
    Element.init();
    newProject();
    gl = new GLWindow();
    gl.init();
    gl.create(0,"jfVideo",1,1,null);  //invisible window
    GL.glInit();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    tabs = new javax.swing.JTabbedPane();
    panel = new javax.swing.JPanel();
    status = new javax.swing.JLabel();
    canvas = new java.awt.Canvas();

    status.setText("Status : Ready");

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(panelLayout.createSequentialGroup()
        .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(canvas, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    panelLayout.setVerticalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(status, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(canvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
  }// </editor-fold>//GEN-END:initComponents

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private java.awt.Canvas canvas;
  private javax.swing.JPanel panel;
  private javax.swing.JLabel status;
  private javax.swing.JTabbedPane tabs;
  // End of variables declaration//GEN-END:variables

  public static GLWindow gl;

  public void newProject() {
    tabs.add("New Project", new ProjectPanel(null));
  }

  private void saveAs() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.setCurrentDirectory(new File(JF.getUserPath() + "/Videos"));
    javax.swing.filechooser.FileFilter vproj = new javax.swing.filechooser.FileFilter() {
      public boolean accept(File file) {
        if (file.isDirectory()) return true;
        return file.getName().endsWith(".vproj");
      }
      public String getDescription() {
        return "Video Project (*.vproj)";
      }
    };
    chooser.addChoosableFileFilter(vproj);
    chooser.setFileFilter(vproj);
    if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return;
    project.projectFile = chooser.getSelectedFile().getAbsolutePath();
    if (!project.projectFile.endsWith(".vproj")) project.projectFile += ".vproj";
    project.saveConfig();
    int idx = tabs.getSelectedIndex();
    String title = project.projectFile;
    int i1 = title.lastIndexOf("/");
    int i2 = title.lastIndexOf(".");
    tabs.setTitleAt(idx, title.substring(i1+1, i2));
  }

  public void open() {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.setCurrentDirectory(new File(JF.getUserPath() + "/Videos"));
    javax.swing.filechooser.FileFilter vproj = new javax.swing.filechooser.FileFilter() {
      public boolean accept(File file) {
        if (file.isDirectory()) return true;
        return file.getName().endsWith(".vproj");
      }
      public String getDescription() {
        return "Video Project (*.vproj)";
      }
    };
    chooser.addChoosableFileFilter(vproj);
    chooser.setFileFilter(vproj);
    if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
    String fn = chooser.getSelectedFile().getAbsolutePath();
    tabs.add(new ProjectPanel(fn));
    int idx = tabs.getTabCount();
    int i1 = fn.lastIndexOf("/");
    int i2 = fn.lastIndexOf(".");
    tabs.setTitleAt(idx-1, fn.substring(i1+1, i2));
  }

  public void save() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    if (project.projectFile == null) {saveAs(); return;}
    project.saveConfig();
  }

  public void close() {
    int idx = tabs.getSelectedIndex();
    tabs.remove(idx);
    if (tabs.getTabCount() == 0) {
      newProject();
    }
  }

  public void render() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    project.render();
  }

  public void props() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    project.props();
  }

  public void addFolder() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    project.addFolder();
  }

  public void reScan() {
    ProjectPanel project = (ProjectPanel)tabs.getSelectedComponent();
    project.reScan();
  }

  public void setStatus(String msg) {
    status.setText(msg);
  }

  /** Runs a runnable for OpenGL on the EDT  */
  public static void runGL(Runnable run) {
    try {
      java.awt.EventQueue.invokeAndWait(run);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
